#include "udf_global.h"

#if ( UDF_EN_01 > 0 && !defined(UDP_SUB_PROJECT) && !defined(UDT_SUB_PROJECT) && (defined(UDF01_SUB_PROJECT) || defined(UDF_PRJ)) )

#include "udsf.h"

#include "../udsf/udsf_uart.c"
#include "../udsf/udsf_cstdlike.c"
#include "../udsf/udsf_delay.c"
#include "../udsf/udsf_memory_access.c"
#include "../UDF/rom_function2_process.c"
#include "../udsf/udsf_spi.c"
#include "../udsf/udsf_gpio.c"
#include "../udsf/udsf_protocol.c"
#include "../udsf/udsf_lpw.c"
#include "../udsf/udsf_dbscan.c"
#include "../udsf/udsf_data_send.c"
#include "../udsf/udsf_udf_switch.c"
#include "../udsf/udsf_data_acquisition.c"
#include "../udsf/udsf_cfar.c"
#include "../udsf/udsf_bbe.c"
#include "../udsf/udsf_i2c_slave.c"
	
// 基于UDF03/UDF13点云结果继续处理
void UDF01_udf_handle(void){
  
  

	// close wdt
	NVIC->ICER[0] = (1 << ((uint32_t)(WDT_IRQn) & 0x1F));
	SYSC->CLK_EN &= ~WDT_PATTERN_EN_Msk;
	SYSC->CLK_EN |= (WDT_PATTERN_EN_Msk&(WDT_PATTERN_DISABLE<<WDT_PATTERN_EN_Pos)); // Close WDT clock 	
 int page_cnt = 1; 
 uint32_t download_addr = BBE_ABUF0_BASE;
//   udsf_i2c_init();//i2c
//  sUDF_global.i2c_regaa = 0xF1;
  sUDF_global.ota_status = OTA_STATUS_DOWNLOAD;
//	LOG_SEND_STRING(LOG_LEVEL_NOTICE,"\nudf01 startup\n");

//	LOG_SEND_STRING(LOG_LEVEL_EMERG,"\nudf01 demo\n");
  while(1)
  {
       	udsf_i2c_salve_check_run_multidata();
      if(sUDF_global.i2c_reg00 == 1)
      {
        sUDF_global.i2c_regaa = 0xF0;
//        sUDF_global.i2c_reg00 = 0;
        sUDF_global.state = 0; 
        
        sUDF_global.ota_status = OTA_STATUS_IDLE;
      	udsf_udf_switch(3,1,0);
                
        break;
      }
    if( sUDF_global.i2c_regaa == 0xF4){
				udsf_udf_switch(3,1,0);
				WDT_RSTNOW();
				while(1);
			}
		if( sUDF_global.ota_status == OTA_STATUS_DOWNLOAD && sUDF_global.i2c_regaa == 0xF3){
			  sUDF_global.ota_status = OTA_STATUS_OTA_WRITE;
			}
				if( sUDF_global.ota_status == OTA_STATUS_DOWNLOAD &&sUDF_global.i2c_regaa != 0xF7){

				#if OTA_SIZE_MAX > BBE_SRAM_SIZE // 本demo 仅演示 小于40KB OTA应用。
				#error OTA_SIZE_MAX > BBE_SRAM_SIZE
				#endif
				
				/* 仅演示验证使用，真实OTA下载流程客户自行实现 */
				#if defined(FAKE_RECV) // fake download. copy current area -> sram(BBE 40K)
				udsf_uart_send_string("[OTA Demo]: FAKE RECV...\n");
				
				// 读当前程序到 BBE_SRAM
				udsf_memory_load2sram(USRBOOT_UDP_DEFAULT_OFFSET+(paramFunc->ab_area_flag-1)*OTA_SIZE_MAX,p_gb_sysboot->boot_flag,p_gb_sysboot->udt_src,\
				(uint8_t*)download_addr,OTA_SIZE_MAX);
				
				sUDF_global.ota_status = OTA_STATUS_OTA_WRITE;
				sUDF_global.ota_len = OTA_SIZE_MAX;
				#else
				// demo uart-ota					
          
				uint8_t* pData8    = (uint8_t *)(sUDF_global.recv_buff);
				if(page_cnt == sUDF_global.binpage_cnt){
					for(int sCnt=0;sCnt<PAGE_PACK_LEN;sCnt++){							
							pData8[sCnt]=sUDF_global.i2c_regbb[sCnt];
					}
           sUDF_global.i2c_regaa = sUDF_global.binpage_cnt;
					if( (pData8[0] == 'A') && (pData8[1] == 'T') && (pData8[2] == '+') &&
						  (pData8[3] == 'C') && (pData8[4] == 'R') && (pData8[5] == 'C') ){
						
								uint16_t crc = 1;
						for(int sCnt=17;sCnt<PAGE_PACK_LEN;sCnt++){
							if( pData8[sCnt] != 0xA5 ){
								crc = 0;
							}
						}
						if( crc == 1 ){
							
							uint32_t len = (pData8[3+11]<<24) | (pData8[2+11]<<16) | (pData8[1+11]<<8) | pData8[0+11];
							
							if( len <= BBE_SRAM_SIZE ){
								crc = pData8[1+15]<<8 | pData8[0+15];

								uint16_t Crc16 = udsf_get_crc((uint8_t*)download_addr,len);

								if( Crc16 == crc ){
//									udsf_uart_send_string("AT+CRC=OK\n");	
									sUDF_global.ota_status = OTA_STATUS_OTA_WRITE;
									sUDF_global.ota_len = len;
									if(paramFunc->ab_area_flag == 1){
										sUDF_global.i2c_regaa = 0xFA;
									}else if(paramFunc->ab_area_flag == 2){
										sUDF_global.i2c_regaa = 0xFB;
									}
									page_cnt = 1;
//									udsf_delay_ms(20);
//									break;
									GPIO0_2 = 1;
									//while(1);
								}else{
										sUDF_global.ota_status = OTA_STATUS_OTA_WRITE;
										sUDF_global.i2c_regaa = 0xFC;
								}
							}else{
//								udsf_uart_send_string("AT+CRC=ERR\n");
								//while(1);
							}
						}
					}

					udsf_memcpy(pData8,(uint8_t*)download_addr+PAGE_PACK_LEN*(sUDF_global.binpage_cnt-1),PAGE_PACK_LEN);
					
					page_cnt++;
//					udsf_uart_send_string("AT+OK\n");
				}
				#endif
			}
      if( sUDF_global.ota_status == OTA_STATUS_OTA_WRITE && sUDF_global.i2c_regaa == 0xF2){
//				udsf_uart_send_string("[OTA Demo]: WRITE OTA...\n");
				
				// 写新ota固件到 新程序分区
				uint32_t start_addr = USRBOOT_UDP_DEFAULT_OFFSET+(2-paramFunc->ab_area_flag)*OTA_SIZE_MAX;
				uint32_t len = sUDF_global.ota_len;
				udsf_memory_program(start_addr,\
				p_gb_sysboot->boot_flag,p_gb_sysboot->udt_src,(uint8_t*)(download_addr),len);

				sUDF_global.ota_status = OTA_STATUS_INFO_WRITE;
				
				sUDF_global.state=0;
				
			}else if( sUDF_global.ota_status == OTA_STATUS_OTA_WRITE && sUDF_global.i2c_regaa == 0xF3){
				sUDF_global.i2c_regaa = 0xF1;
				sUDF_global.ota_status = OTA_STATUS_DOWNLOAD;
				page_cnt = 1;
				sUDF_global.binpage_cnt = 0;
			}
      if( sUDF_global.ota_status == OTA_STATUS_INFO_WRITE ){
//				udsf_uart_send_string("[OTA Demo]: WRITE INFO...\n");
				uint32_t sn;
				uint32_t sn_bu;
				// 读当前运行分区 sn@INFO
				udsf_memory_load2sram(USRBOOT_INFO_OFFSET+(paramFunc->ab_area_flag-1)*USRBOOT_INFO_SIZE_PRE+UDP_SIZE,p_gb_sysboot->boot_flag,p_gb_sysboot->udt_src,\
				(uint8_t*)&sn,4);
				udsf_memory_load2sram(USRBOOT_INFO_OFFSET+(paramFunc->ab_area_flag-1)*USRBOOT_INFO_SIZE_PRE+UDP_SIZE+4,p_gb_sysboot->boot_flag,p_gb_sysboot->udt_src,\
				(uint8_t*)&sn_bu,4);
				
				if( sn+sn_bu != 0xffffffff ){
					sn = 0; // 当前分区对应INFO区为空（首次OTA 两个INFO区均为空）
				}
				// sn+1
				sn += 1;
				*(uint32_t*)(download_addr+UDP_SIZE)   = sn;
				*(uint32_t*)(download_addr+UDP_SIZE+4) = 0xffffffff - sn;
				
				// 写新分区 INFO
				udsf_memory_program(USRBOOT_INFO_OFFSET+(2-paramFunc->ab_area_flag)*USRBOOT_INFO_SIZE_PRE,\
				p_gb_sysboot->boot_flag,p_gb_sysboot->udt_src,(uint8_t*)(download_addr),FLASH_SECTOR_SIZE);
				
				//udsf_uart_send_string("[OTA Demo]: WILL RESET...\n");
				udsf_udf_switch(3,1,0);
				WDT_RSTNOW();
				while(1);
			}
      
    }
}
#endif
